/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multiface.promonet.servlet.section;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import multiface.promonet.jpa.Lavoro;
import multiface.promonet.jpa.Prodotto;
import multiface.promonet.jpa.Report;
import multiface.promonet.jpa.RigaProdottoReport;
import multiface.promonet.jpa.util.PersistenceManager;
import multiface.web.util.ServletUtil;

/**
 *
 * @author diego
 */
public class Job {
    public static void dispatcher(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String action = request.getParameter("action");
        if (action.equalsIgnoreCase("del_report_row")) {
            Job.delReportRow(request, response);
        }
        if (action.equalsIgnoreCase("add_report_row")) {
            Job.addReportRow(request, response);
        }
        if (action.equalsIgnoreCase("mod_report_row")) {
            Job.modReportRow(request, response);
        }
    }
    
    
      /**
     * Richiesta Inserimento di una riga nel report del Promoter Loggato
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public static void addReportRow(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession s = request.getSession();

        if (ServletUtil.userIsLogged(s)) {

            EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
            EntityManager em = emf.createEntityManager();

            Prodotto p = em.find(Prodotto.class, Integer.parseInt(request.getParameter("prodottoid")));
            Report r = em.find(Report.class, Integer.parseInt(request.getParameter("reportid")));
            Lavoro l = em.find(Lavoro.class, Integer.parseInt(request.getParameter("lavoroid")));

            RigaProdottoReport riga = new RigaProdottoReport();
            riga.setPrezzo(new BigDecimal(request.getParameter("prezzo")));
            riga.setProdotto(p);
            riga.setReport(r);
            riga.setStock(Integer.parseInt(request.getParameter("stock")));
            riga.setVenduti(Integer.parseInt(request.getParameter("venduti")));

            l.getReport().setStato("APERTO");

            em.getTransaction().begin();
            em.persist(riga);
            em.persist(l);
            em.getTransaction().commit();

            PrintWriter out = response.getWriter();
            out.print(riga.getIdRigaProdottoReport());

            em.close();
        }
    }

    /**
     * Richiesta di cancellazione di una riga del report del Promoter Loggato
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public static void delReportRow(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession s = request.getSession();
        if (ServletUtil.userIsLogged(s)) {
            PrintWriter out = response.getWriter();

            EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
            EntityManager em = emf.createEntityManager();

            RigaProdottoReport riga = em.find(RigaProdottoReport.class, Integer.parseInt(request.getParameter("rigaid")));

            em.getTransaction().begin();
            em.remove(riga);
            em.getTransaction().commit();

            em.close();
        }
    }

    /**
     * Richiesta di modifica di una riga del report del Promoter Loggato
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public static void modReportRow(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession s = request.getSession();
        if (ServletUtil.userIsLogged(s)) {
            EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
            EntityManager em = emf.createEntityManager();

            RigaProdottoReport riga = em.find(RigaProdottoReport.class, Integer.parseInt(request.getParameter("rigaid")));
            
            String prezzo = request.getParameter("prezzo");
            
            riga.setPrezzo(new BigDecimal(prezzo));
            riga.setStock(Integer.parseInt(request.getParameter("stock")));
            riga.setVenduti(Integer.parseInt(request.getParameter("venduti")));

            em.getTransaction().begin();
            em.persist(riga);
            em.getTransaction().commit();
            
            PrintWriter out = response.getWriter();
            out.print(riga.getIdRigaProdottoReport());

            em.close();
        }
    }
}
